package com.edu.zju.scs.panacea.service.exposure;

import com.alibaba.fastjson.JSON;
import com.edu.zju.scs.panacea.common.Global;
import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.exposure.*;
import com.edu.zju.scs.panacea.entity.plan.Task;
import com.edu.zju.scs.panacea.entity.query.ExposureQuery;
import com.edu.zju.scs.panacea.entity.resource.Dev;
import com.edu.zju.scs.panacea.mapper.exposure.CrowdPortrayalMapper;
import com.edu.zju.scs.panacea.mapper.exposure.ExposureMapper;
import com.edu.zju.scs.panacea.mapper.plan.TaskMapper;
import com.edu.zju.scs.panacea.mapper.resource.DevMapper;
import com.edu.zju.scs.panacea.utils.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Date;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-02-08 10:59
 */
@Service
public class ExposureService {
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private CrowdPortrayalMapper crowdPortrayalMapper;

    @Autowired
    private DevMapper devMapper;

    @Autowired
    private ExposureMapper exposureMapper;

    public void handleWifiData(UploadData uploadData) {
        // 处理曝光量
        Task task = taskMapper.selectTask(uploadData.getTaskId());
        Long planId = task.getPlanId();
        // 过滤wifi数据，找到指定范围内的mac地址
        List<TerminalInfo> terminalInfos = uploadData.getTerminalInfos();
        Exposure exposure = new Exposure();
        for (TerminalInfo terminalInfo : terminalInfos) {
            if (terminalInfo.getDistance() > 50) {
                continue;
            }
            // 查询人的画像
            CrowdPortrayal crowdPortrayal = crowdPortrayalMapper.selectCrowdPortrayal(terminalInfo.getTerminalMac());
            // 如果有线上画像
            if (crowdPortrayal != null) {
                exposure.setPlanId(planId);
                exposure.addAgeNum(crowdPortrayal.getAge());
                exposure.setExposureDate(new Date(uploadData.getTransmitTime().getTime()));
                exposure.addMarriageNum(crowdPortrayal.getMarriage());
                exposure.addSexNum(crowdPortrayal.getSex());
                exposure.addTodayAmount();
                // 加入到map中
                if (!Global.exposureHashMap.containsKey(planId)) {
                    Global.exposureHashMap.put(planId, exposure);
                } else {
                    Exposure exsitExposure = Global.exposureHashMap.get(planId);
                    exsitExposure.add(exposure);
                    Global.exposureHashMap.put(planId, exsitExposure);
                }
                ExposureQuery exposureQuery = new ExposureQuery();
                exposureQuery.setPlanId(planId);
                Exposure exposureDate = exposureMapper.selectExposureDate(exposureQuery).get(0);
                if (exposureDate == null) {
                    exposure.setTotalAmount(exposure.getTodayAmount() + 10);
                    exposureMapper.insertExposureDate(exposure);
                } else {
//                    exposureMapper.updateExposureDate(exposure);
                }

                Exposure exposureTotal = exposureMapper.selectExposureTotal(planId);
                if (exposureTotal == null) {
                    exposureMapper.insertExposureTotal(exposure);
                } else {
                    exposureMapper.updateExposureTotal(exposure);
                }

            }

        }

        // 处理大屏定向
        // 找到设备
        Dev dev = devMapper.selectDevByProbe(uploadData.getDeviceId());
        for (TerminalInfo terminalInfo : terminalInfos) {
            // 根据mac地址获取到人群画像
            // 查询人的画像
            CrowdPortrayal crowdPortrayal = crowdPortrayalMapper.selectCrowdPortrayal(terminalInfo.getTerminalMac());
            // 如果有线上画像
            if (crowdPortrayal != null) {
                DevPortrayalData devPortrayalData = new DevPortrayalData();
                devPortrayalData.addSexNum(crowdPortrayal.getSex());
                devPortrayalData.addAgeNum(crowdPortrayal.getAge());
                devPortrayalData.addMarriageNum(crowdPortrayal.getMarriage());
                devPortrayalData.addEducationNum(crowdPortrayal.getEducation());
                devPortrayalData.setCar(crowdPortrayal.getCar());
                devPortrayalData.setGame(crowdPortrayal.getGame());
                devPortrayalData.setInfantMom(crowdPortrayal.getInfantMom());
                devPortrayalData.setLife(crowdPortrayal.getLife());
                devPortrayalData.setManageMoney(crowdPortrayal.getManageMoney());
                devPortrayalData.setRecreation(crowdPortrayal.getRecreation());
                devPortrayalData.setTrip(crowdPortrayal.getTrip());
                devPortrayalData.setSport(crowdPortrayal.getSport());
                devPortrayalData.setShopping(crowdPortrayal.getShopping());
                devPortrayalData.setSocialContact(crowdPortrayal.getSocialContact());
                // 加入到map中
                if (!Global.devPortrayalDataHashMap.containsKey(dev.getId())) {
                    Global.devPortrayalDataHashMap.put(dev.getId(), devPortrayalData);
                } else {
                    DevPortrayalData exsitDevPortrayalData = Global.devPortrayalDataHashMap.get(dev.getId());
                    exsitDevPortrayalData.add(devPortrayalData);
                    Global.devPortrayalDataHashMap.put(planId, exsitDevPortrayalData);
                }
                System.out.println("########################################################");
                System.out.println(JSON.toJSONString(Global.devPortrayalDataHashMap));
            }


        }

    }

    public BaseResult getExposureAmount(Long planId) {
        BaseResult baseResult = new BaseResult();
        Long time = System.currentTimeMillis();
        Date startDate = new Date(time - MathUtil.timeMillis(10));
        Date endDate = new Date(time - MathUtil.timeMillis(1));
        ExposureQuery exposureQuery = new ExposureQuery();
        exposureQuery.setPlanId(planId);
        exposureQuery.setStartDate(startDate);
        exposureQuery.setEndDate(endDate);
        List<Exposure> exposures = exposureMapper.selectExposureDate(exposureQuery);
        baseResult.success(exposures);
        return baseResult;
    }

    public BaseResult getExposureComparison(Long planId) {
        BaseResult baseResult = new BaseResult();
        Exposure exposure = exposureMapper.selectExposureTotal(planId);
        baseResult.success(exposure);
        return baseResult;
    }


}
